package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("DetectdetailController")
public class DetectdetailController extends CmiEmaController {

	@RequestMapping("/listDetectdetails")
	public ModelAndView listDetectdetails(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = detectdetailPaginationService.updatePagination(request, pagination);
		Set<Detectdetail> detectdetails = detectdetailPaginationService.getPaginationDetectdetails(pagination);

		mav.addObject("detectdetails", detectdetails);
		mav.addObject("entityName", "detectdetail");
		mav.addObject("pagination", pagination);
		mav.setViewName("detectdetail/listDetectdetails");
		return mav;
	}

	public String listDetectdetails() {
		return "redirect:/listDetectdetails";
	}

	@RequestMapping("/viewDetectdetail")
	public ModelAndView viewDetectdetail(@RequestParam Integer detectdetailId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("detectdetail", detectdetailDAO.findDetectdetailByPrimaryKey(detectdetailId));
		mav.setViewName("detectdetail/viewDetectdetail");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteDetectdetails")
	public ModelAndView confirmDeleteDetectdetails(
			@RequestParam(value = "deleteDetectdetailsIds", required = true) Set<Integer> deleteDetectdetailsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Detectdetail> deleteDetectdetails = detectdetailDAO.findDetectdetailsByIds(deleteDetectdetailsIds);
		Set<Detectdetail> referencedDetectdetails = detectdetailService.loadReferenceDetectdetails(deleteDetectdetails);
		boolean hasReferenced;
		if (referencedDetectdetails.size() == 0) {
			mav.addObject("deleteEntitys", deleteDetectdetails);
			hasReferenced = false;
		} else {
			deleteDetectdetails.removeAll(referencedDetectdetails);
			mav.addObject("referencedEntitys", referencedDetectdetails);
			mav.addObject("deleteEntitys", deleteDetectdetails);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Detectdetail");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listDetectdetails");
		return mav;
	}

	@RequestMapping("/deleteDetectdetail")
	public String deleteDetectdetail(@RequestParam Integer detectdetailId) {
		Detectdetail detectdetail = detectdetailDAO.findDetectdetailByPrimaryKey(detectdetailId);
		detectdetailService.deleteDetectdetail(detectdetail);
		return "forward:/listDetectdetails";
	}

	@RequestMapping("/confirmDeleteDetectdetail")
	public ModelAndView confirmDeleteDetectdetail(@RequestParam Integer detectdetailId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("detectdetail", detectdetailDAO.findDetectdetailByPrimaryKey(detectdetailId));

		mav.setViewName("detectdetail/deleteDetectdetail");

		return mav;
	}

	@RequestMapping("/deleteDetectdetails")
	public String deleteDetectdetails(
			@RequestParam(value = "deleteDetectdetailsId", required = true) Set<Integer> deleteDetectdetailsId) {
		detectdetailService.deleteDetectdetails(deleteDetectdetailsId);
		return "forward:/listDetectdetails";
	}

	@RequestMapping("/editDetectdetail")
	public ModelAndView editDetectdetail(@RequestParam Integer detectdetailId) {

		ModelAndView mav = new ModelAndView();

		Detectdetail detectdetail = detectdetailDAO.findDetectdetailByPrimaryKey(detectdetailId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("detectdetail", detectdetail);

		// 把所有的引用detectdetail对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(detectdetailService.loadDetectdetailObjectReferencedSet(detectdetail));

		// 将编辑页面detectdetail添加到mav中
		mav.setViewName("detectdetail/editDetectdetail");
		return mav;
	}

	@RequestMapping("/newDetectdetail")
	public ModelAndView newDetectdetail() {
		ModelAndView mav = new ModelAndView();
		Detectdetail detectdetail = new Detectdetail();

		mav.addObject("detectdetail", detectdetail);

		// 把所有的引用detectdetail对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(detectdetailService.loadDetectdetailObjectReferencedSet(detectdetail));

		mav.addObject("newFlag", true);
		mav.setViewName("detectdetail/editDetectdetail");

		return mav;
	}

	@RequestMapping("/saveDetectdetail")
	public ModelAndView saveDetectdetail(@ModelAttribute Detectdetail detectdetail) {

		ModelAndView mav = new ModelAndView();

		Detect detectTemp = detectdetail.getDetect();
		Detect detect = detectTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(detectDAO.findDetectById(detectTemp.getId()));
		Feature featureTemp = detectdetail.getFeature();
		Feature feature = featureTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(featureDAO.findFeatureById(featureTemp.getId()));
		Unit unitTemp = detectdetail.getUnit();
		Unit unit = unitTemp == null ? null : CmiHibernateProxyUtil.deproxy(unitDAO.findUnitById(unitTemp.getId()));
		Detectmethod detectmethodTemp = detectdetail.getDetectmethod();
		Detectmethod detectmethod = detectmethodTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(detectmethodDAO.findDetectmethodById(detectmethodTemp.getId()));

		Detectdetail existingDetectdetail = CmiHibernateProxyUtil
				.deproxy(detectdetailDAO.findDetectdetailByPhysicalPrimaryKey(detect.getId(), feature.getId(),
						detectdetail.getValue(), unit.getId()));

		detectdetail.setDetect(detect);
		detectdetail.setFeature(feature);
		detectdetail.setUnit(unit);
		detectdetail.setDetectmethod(detectmethod);
		// 新建记录
		if (detectdetail.getId() == null) {
			if (detectdetail.equals(existingDetectdetail)) {

				mav.addAllObjects(detectdetailService.loadDetectdetailClassReferencedSet(detectdetail));
				// mav = editContinue(mav, detectdetail);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("detectdetail/editDetectdetail");
			} else {
				detectdetailService.saveDetectdetail(detectdetail);
				mav.setViewName("forward:/listDetectdetails");
			}
		}
		// 编辑记录
		else {
			Detectdetail originDetectdetail = CmiHibernateProxyUtil
					.deproxy(detectdetailDAO.findDetectdetailByPrimaryKey(detectdetail.getId()));
			if (!detectdetail.equals(originDetectdetail) && detectdetail.equals(existingDetectdetail)) {
				mav.addAllObjects(detectdetailService.loadDetectdetailClassReferencedSet(detectdetail));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("detectdetail/editDetectdetail");
			} else {
				detectdetailService.saveDetectdetail(detectdetail);
				mav.setViewName("forward:/listDetectdetails");
			}
		}
		return mav;
	}

	@RequestMapping("/detectdetailController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
